package com.mine.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mine.pojo.Cart;
import com.mine.pojo.Meal;
import com.mine.pojo.OrderItem;
import com.mine.pojo.User;
import com.mine.service.CartService;
import com.mine.service.MealService;
import com.mine.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class MealController {

    @Autowired
    MealService mealService;
    @Autowired
    CartService cartService;
    @Autowired
    OrderService orderService;

    //*点餐页面
    @RequestMapping("shopList")
    public String shopList(String select_name, Model model, String class0, String class1, @RequestParam(defaultValue = "1") Integer pageNum) {

        //分页，每页4个
        PageHelper.startPage(pageNum, 4);
        if (class0 == null || "".equals(class0)) {
            class0 = "0";
        }
        if (class1 == null || "".equals(class1)) {
            class1 = "0";
        }
        if(select_name == null){
            select_name = "";
        }
        List<Meal> mealList = mealService.shopList(class0, class1, select_name);
        //把查询结果放里面就可以了
        PageInfo<Meal> pageInfo = new PageInfo<>(mealList);
        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("class0", class0);
        model.addAttribute("class1", class1);
        List<Meal> meals = mealService.queryAllMeal();//卖出最多的商品
        List<Meal> mealList1 = meals.subList(0, 2);
        model.addAttribute("mealList", mealList1);
        model.addAttribute("select_name", select_name);
        return "meal/shop_list";
    }

    //*添加商品
    @RequestMapping("/add_meal")
    @ResponseBody
    public String add_meal(Integer meal_id, HttpSession session, Model model) {
        User user = (User) session.getAttribute("user");
        if (user == null) { //如果找不到用户，转到登录页
            model.addAttribute("msg", "请先登录");
            return "login";
        }
        Meal meal = mealService.getMealById(meal_id);
        orderService.addOrderItem(meal, session);
        return "OK";
    }

    //*展示菜色信息
    @GetMapping("/shop")
    public String shop(Model model, Integer meal_id, HttpSession session) {
        User user = (User) session.getAttribute("user");
        if (user == null) { //如果找不到用户，转到登录页
            model.addAttribute("msg", "请先登录");
            return "login";
        }
        List<Meal> meals = mealService.queryAllMeal();//卖出最多的商品
        List<Meal> mealList = meals.subList(0, 2);
        Meal meal = mealService.getMealById(meal_id);
        //查找食品相应评论
        List<Cart> cartList = cartService.queryAllCartByMealId(meal_id);
        model.addAttribute("mealshop", meal);
        model.addAttribute("mealList", mealList);
        model.addAttribute("cartList", cartList);
        if (user.getCollect().contains(meal_id.toString())) {
            model.addAttribute("collect", "collect");
        }
        return "meal/shop";
    }

    //*收藏
    @RequestMapping("/mealCollect")
    @ResponseBody
    public String mealCollect(Integer meal_id, HttpSession session, Model model) {
        User user = (User) session.getAttribute("user");
        if (user == null) { //如果找不到用户，转到登录页
            model.addAttribute("msg", "请先登录");
            return "login";
        }
        mealService.mealCollect(meal_id, session);
        model.addAttribute("collect", "");
        return "OK";
    }

    //*取消收藏
    @RequestMapping("/mealCollectOver")
    @ResponseBody
    public String mealCollectOver(Integer meal_id, HttpSession session, Model model) {
        User user = (User) session.getAttribute("user");
        if (user == null) { //如果找不到用户，转到登录页
            model.addAttribute("msg", "请先登录");
            return "login";
        }
        mealService.mealCollectOver(meal_id, session);
        model.addAttribute("collect", "");
        return "OK";
    }

    //*评论页面
    @GetMapping("comment")
    public String comment(String meal_id, String orderitem_id, Model model, HttpSession session, String order_id) {
        User user = (User) session.getAttribute("user");
        if (user == null) { //如果找不到用户，转到登录页
            model.addAttribute("msg", "请先登录");
            return "login";
        }
        Cart cart = cartService.queryCartByUserMealOrderItem(user.getUser_id(), meal_id, orderitem_id);
        if (cart != null) {
            return "redirect:/LookOrder?invalid=invalid&full=0&order_id=" + order_id;
        }
        Meal meal = mealService.getMealById(Integer.valueOf(meal_id));
        OrderItem orderItem = orderService.getOrderitemById(orderitem_id);
        model.addAttribute("meal", meal);
        model.addAttribute("orderitem", orderItem);
        return "user/user_collect";
    }

    //*评论
    @PostMapping("Usermealcollect")
    public String Usermealcollect(String meal_id, String orderItem_id, String title, Model model, HttpSession session) {
        User user = (User) session.getAttribute("user");
        if (user == null) { //如果找不到用户，转到登录页
            model.addAttribute("msg", "请先登录");
            return "login";
        }
        cartService.addCart(meal_id, orderItem_id, title, user.getUser_id());
        return "redirect:/user_message?mm=0";
    }
}
